package zsc.nine.onlinemall.service;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import zsc.nine.onlinemall.entity.Order;
import zsc.nine.onlinemall.entity.User;
import zsc.nine.onlinemall.mapper.OrderMapper;
import zsc.nine.onlinemall.mapper.UserMapper;
import zsc.nine.onlinemall.utils.SendMailTool;

import java.util.Date;
import java.util.List;

/**
 * @ClassName: OrderService
 * @Description: Service of Order operation
 * @Author: Zainus
 * @CreateDate: Jul.23, 2021
 * @UpdateDate: Jul.23, 2021
 * @Version: [v1.0]
 */

/**
 * @类名: OrderService
 * @描述: 订单管理服务类
 * @创建人: 叶增钥
 * @创建时间: 2021年7月23日
 * @更新时间: 2021年7月23日
 * @版本: [v1.0]
 */
@Component
public class OrderService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    SendMailTool sendMailTool;

    public boolean createOrder(String Sum, String Receiver, String Address, String Mobile, String ItemNumber, String ProductId, String CreatorEmail) {

        Order order = new Order();
        order.setSum(Double.parseDouble(Sum));
        order.setReceiver(Receiver);
        order.setAddress(Address);
        order.setMobile(Mobile);
        order.setCreateDate(new Date(System.currentTimeMillis()));
        order.setStatus(0);
        order.setItemNumber(Integer.parseInt(ItemNumber));
        order.setBProductId(Integer.parseInt(ProductId));
        order.setCreatorEmail(CreatorEmail);

        //System.out.println(order);

        return orderMapper.insertOrder(order);

    }

    public boolean deliver(String OrderId) {

        Order order = orderMapper.findOrderById(Integer.parseInt(OrderId));
        User user = userMapper.findByEmail(order.getCreatorEmail());
        if (user != null) { //order != null
            try {
                sendMailTool.sendMail("0", user.getUserEmail(), user.getUserName()); //0为发货 1为退款
                order.setStatus(1);
                orderMapper.updateStatusById(order.getId(), order.getStatus());
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        return false;
    }

    public boolean refund(String OrderId) {

        Order order = orderMapper.findOrderById(Integer.parseInt(OrderId));
        User user = userMapper.findByEmail(order.getCreatorEmail());
        if (user != null) { //order != null
            try {
                sendMailTool.sendMail("1", user.getUserEmail(), user.getUserName()); //0为发货 1为退款
                order.setStatus(2);
                orderMapper.updateStatusById(order.getId(), order.getStatus());
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        return false;
    }

    public String getLength() {
        return orderMapper.getLength();
    }

    public boolean orderExist(String OrderId) {
        return orderMapper.findOrderById(Integer.parseInt(OrderId)) != null;
    }

    public Order findOrderById(String OrderId) {
        return orderMapper.findOrderById(Integer.parseInt(OrderId));
    }

    public boolean deleteOrderById(String OrderId) {
        return orderMapper.deleteOrder(Integer.parseInt(OrderId));
    }

    public List<Order> pagedFindById(String index, String pageSize) {
        return orderMapper.pagedFindById(Integer.parseInt(index), Integer.parseInt(pageSize));
    }

    public List<Order> pagedFindByCreatorEmail(String index, String pageSize, String CreatorEmail) {
        return orderMapper.pagedFindByCreatorEmail(Integer.parseInt(index), Integer.parseInt(pageSize),CreatorEmail);
    }

    public List<Order> findAll(){
        List <Order> orderList =  orderMapper.findAllOrder();
        return orderList;
    }

}
